keep = ls()


##############
#Prepare Data#
##############

#Load township data
ia_twp_table = fread('./data/suffrage_referenda/cleaned/ia_twp_analysis_cleaned.csv')
wi_twp_table = fread('./data/suffrage_referenda/cleaned/wi_twp_analysis_cleaned.csv')

#Make variables: Iowa
#####################

#Enlistment rate
ia_twp_table[, vet_pct := vets_alt / elig_1868]
ia_twp_table[, vet_pct_s := vets_alt_s / elig_1868]

#Suffrage vote-share
ia_twp_table[, suff_57_yes := suff_yes_57 / (elig_1868)]
ia_twp_table[, suff_65_yes := suff_yes_68 / elig_1868]
ia_twp_table[, suff_57_no := suff_no_57 / (elig_1868)]
ia_twp_table[, suff_65_no := suff_no_68 / elig_1868]
ia_twp_table[, diff_suff := suff_65_yes - suff_57_yes]

#Eligible voter
ia_twp_table[, elig_pct := elig_1857 / elig_1868]
ia_twp_table[, change := elig_1868_70 / elig_1868]

#Estimation sample
ia_twp_table[, sample_1 := vet_pct < 0.6 & suff_57_yes < 1 & suff_65_yes < 1 ]
ia_twp_table[, sample_2 := vet_pct < 0.6 & suff_57_yes < 1 & suff_65_yes < 1 & change %between% c(0.5,1.5)]

#Make variables: Wisconsin
##########################

#Enlistment rate
wi_twp_table[, vet_pct := vets_alt / elig_1865]
wi_twp_table[, vet_pct_s := vets_alt_s / elig_1865]
wi_twp_table[, vet_pct_h := vets_alt_h / elig_1865 ]

#Suffrage Vote
wi_twp_table[, suff_57_yes := suff_yes_1857 / (elig_1865)]
wi_twp_table[, suff_65_yes := suff_yes_1865 / elig_1865]
wi_twp_table[, suff_57_no := suff_no_1857 / (elig_1865)]
wi_twp_table[, suff_65_no := suff_no_1865 / elig_1865]
wi_twp_table[, diff_suff := suff_65_yes - suff_57_yes]

#Gubernatorial Vote
wi_twp_table[, gov_57_d := gov_d_1857 / elig_1865]
wi_twp_table[, gov_65_d := gov_d_1865 / elig_1865]
wi_twp_table[, gov_57_r := gov_r_1857 / elig_1865]
wi_twp_table[, gov_65_r := gov_r_1865 / elig_1865]
wi_twp_table[, gov_57_r_diff := gov_57_r - suff_57_yes]

#Eligible Voters
wi_twp_table[, elig_pct := elig_1857 / elig_1865]
wi_twp_table[, change := elig_1865_70 / elig_1865]

#Turnout
wi_twp_table[, turnout_65 := (suff_yes_1865 + suff_no_1865)/elig_1865]
wi_twp_table[, turnout_57 := (suff_yes_1857 + suff_no_1857)/elig_1865]
wi_twp_table[, gov_to_1865 := gov_r_1865 + gov_d_1865]
wi_twp_table[, suff_to_1865 := suff_yes_1865 + suff_no_1865]
wi_twp_table[, to_1865 := .SD %>% as.matrix %>% rowMaxs(), .SDcols = c('gov_to_1865', "suff_to_1865")]
wi_twp_table[, gov_to_1857 := gov_r_1857 + gov_d_1857]
wi_twp_table[, suff_to_1857 := suff_yes_1857 + suff_no_1857]
wi_twp_table[, to_1857 := .SD %>% as.matrix %>% rowMaxs(), .SDcols = c('gov_to_1857', "suff_to_1857")]

#Estimation Sample:
wi_twp_table[, use_unit := suff_yes_1865 >=0 & 
               suff_no_1865 >= 0 & 
               gov_r_1865 >= 0 & 
               gov_d_1865 >= 0 &
               (suff_yes_1865 + suff_no_1865 + gov_r_1865 + gov_d_1865) > 0]
wi_twp_table[, sample_1 := use_unit & vet_pct < 1 & suff_57_yes < 1 & suff_65_yes < 1 ]
wi_twp_table[, sample_2 := use_unit & vet_pct < 1 & suff_57_yes < 1 & suff_65_yes < 1 & change %between% c(0.5,1.5)]

#Combine Data
#############

ia_wi_twp_table = rbind(ia_twp_table[, list(unit_id = paste0("ia_", ia_unit_id),
                                            state = "IOWA", 
                                            county = County,
                                            unit_type,
                                            vet_pct, 
                                            vet_pct_s,
                                            suff_57_yes, suff_65_yes, diff_suff,
                                            suff_57_no, suff_65_no,
                                            elig_pct, change,
                                            sample_1, sample_2,
                                            elig_1865 = elig_1868,
                                            c_company_casualty_rate,
                                            c_company_kia,
                                            c_combat_days, 
                                            c_battles,
                                            c_usct_combat_days,
                                            c_usct_brigade_any
                                            )],
                        wi_twp_table[, list(unit_id = paste0("wi_", wi_unit_id),
                                            state = "WISCONSIN", 
                                            county = County_Cluster,
                                            unit_type,
                                            vet_pct, 
                                            vet_pct_s,
                                            suff_57_yes, suff_65_yes, diff_suff,
                                            suff_57_no, suff_65_no,
                                            elig_pct, change,
                                            sample_1, sample_2,
                                            elig_1865,
                                            c_company_casualty_rate,
                                            c_company_kia,
                                            c_combat_days, 
                                            c_battles,
                                            c_usct_combat_days,
                                            c_usct_brigade_any
                        )])


#########
#Table 3#
#########

referenda_lm_ia_wi = list(
  lm(suff_65_yes ~ vet_pct + suff_57_yes*state, data = ia_wi_twp_table[(sample_1)], weights = ia_wi_twp_table[(sample_1), elig_1865]),
  lm(diff_suff ~ vet_pct + state , data = ia_wi_twp_table[(sample_1)], weights = ia_wi_twp_table[(sample_1), elig_1865]),
  lm(diff_suff ~ vet_pct + elig_pct + state, data = ia_wi_twp_table[(sample_1)], weights = ia_wi_twp_table[(sample_1), elig_1865]),
  lm(suff_65_yes ~ vet_pct + suff_57_yes*state, data = ia_wi_twp_table[(sample_2)], weights = ia_wi_twp_table[(sample_2), elig_1865]),
  lm(diff_suff ~ vet_pct + state , data = ia_wi_twp_table[(sample_2)], weights = ia_wi_twp_table[(sample_2), elig_1865]),
  lm(diff_suff ~ vet_pct + elig_pct + state, data = ia_wi_twp_table[(sample_2)], weights = ia_wi_twp_table[(sample_2), elig_1865])
)

table_3 = stargazer(referenda_lm_ia_wi,
                          se = lapply(referenda_lm_ia_wi, function(x) x %>% vcovHC(., "HC3") %>% diag %>% sqrt),
                          type = 'latex',
                          keep.stat = 'n',
                          digits = 3,
                          column.labels = c('Full Sample','Restricted Sample'),
                          column.separate = c(3,3),
                          dep.var.labels = c("Yes (Post)", "Yes (Diff.)") %>% rep(2),
                          keep = c('vet_pct'),
                          covariate.labels = 'Enlistment Rate',
                          add.lines = list(c('Lagged DV', rep(c("Y", "N", "N"), 2)),
                                           c("Differenced", rep(c("N", "Y", "Y"), 2)),
                                           c("Controls", rep(c("N", "N", "Y"), 2))
                          ),
                          label = "tab:twp_suffrage_main_table",
                          title = "Effect of Enlistment on Support for Black Suffrage (Iowa and Wisconsin Township Returns)",
                          star.cutoffs = c(0.05,0.01,0.001)
)

note = 'Enlistment rate is number of men serving over those eligible to vote in 1865 (WI) or 1868 (IA). Suffrage vote totals come from state constitutional referenda in stable clusters of townships/counties in Iowa and Wisconsin. All models include state fixed effects. Lagged dependent variables are allowed different slopes by state. Control variables include: (i) the fraction eligible to vote in 1857 over those eligible to vote in the post-war referenda. Townships are weighted by number of white men. The restricted sample includes only townships where the population eligible to vote in 1865/1868 changed by less than 50 percent between 1860 and 1870. Standard errors are robust.'
write_latex(table_3, note, './output/paper/Table_3.tex')

#table 3 full
names(referenda_lm_ia_wi) = c("Yes (Post)", "Yes (Diff.)",  "Yes (Diff.)", "Yes (Post)", "Yes (Diff.)","Yes (Diff.)")

rows = rbind(c('Lagged DV', rep(c("Y", "N", "N"), 2)),
             c("Differenced", rep(c("N", "Y", "Y"), 2)),
             c("Controls", rep(c("N", "N", "Y"), 2)) 
) %>% as.data.frame()

attr(rows, 'position') = 13:15

table_out = modelsummary(referenda_lm_ia_wi,
                         coef_rename = c('vet_pct' = 'Enlistment Rate', 
                                         'suff_57_yes' = "Pro Suffrage (%) 1857",
                                         'stateWISCONSIN' = "Wisconsin",
                                         'suff_57_yes:stateWISCONSIN' = 'Pro Suffrage (%) 1857 x Wisconsin',
                                         'elig_pct' = "Ratio of Elig. Voters (Pre/Post)"),
                         notes = note,
                         stars = T,
                         vcov = 'HC3',
                         add_rows = rows,
                         gof_omit = "(Log)|(AIC)|(BIC)",
                         title = "Effect of Enlistment on Support for Black Suffrage (Iowa and Wisconsin Township Returns)") %>%
  add_header_above(c(" " = 1, "Full Sample" = 3, "Restricted Sample" = 3))
table_out = data.table(table = table_out, name = "Table 3")
table_list = c(table_list, list(table_out))




##########
#Table C1#
##########

referenda_lm_wi = list(
  lm(suff_65_yes ~ vet_pct + suff_57_yes, data = wi_twp_table[(sample_1)], weights = wi_twp_table[(sample_1), elig_1865]),
  lm(suff_65_yes ~ vet_pct + suff_57_yes + gov_57_r_diff + elig_pct, data = wi_twp_table[(sample_1)], weights = wi_twp_table[(sample_1), elig_1865]),
  lm(diff_suff ~ vet_pct, data = wi_twp_table[(sample_1)], weights = wi_twp_table[(sample_1), elig_1865]),
  lm(diff_suff ~ vet_pct + gov_57_r_diff + elig_pct, data = wi_twp_table[(sample_1)], weights = wi_twp_table[(sample_1), elig_1865]),
  lm(diff_suff ~ vet_pct + gov_57_r_diff + elig_pct + County_Cluster, data = wi_twp_table[(sample_1)], weights = wi_twp_table[(sample_1), elig_1865]),
  lm(suff_65_yes ~ vet_pct + suff_57_yes, data = wi_twp_table[(sample_2)], weights = wi_twp_table[(sample_2), elig_1865]),
  lm(suff_65_yes ~ vet_pct + suff_57_yes + gov_57_r_diff + elig_pct, data = wi_twp_table[(sample_2)], weights = wi_twp_table[(sample_2), elig_1865]),
  lm(diff_suff ~ vet_pct, data = wi_twp_table[(sample_2)], weights = wi_twp_table[(sample_2), elig_1865]),
  lm(diff_suff ~ vet_pct + gov_57_r_diff + elig_pct, data = wi_twp_table[(sample_2)], weights = wi_twp_table[(sample_2), elig_1865]),
  lm(diff_suff ~ vet_pct + gov_57_r_diff + elig_pct + County_Cluster, data = wi_twp_table[(sample_2)], weights = wi_twp_table[(sample_2), elig_1865])
)

table_c1 = stargazer(referenda_lm_wi,
                               se = lapply(referenda_lm_wi, function(x) x %>% vcovHC(., "HC1") %>% diag %>% sqrt),
                               type = 'latex',
                               keep.stat = 'n',
                               digits = 3,
                               column.labels = c('Full Sample','Restricted Sample'),
                               column.separate = c(5,5),
                               dep.var.labels = rep('(Yes/Elig.)', 10),
                               keep = c('vet_pct'),
                               covariate.labels = 'Enlistment Rate',
                               add.lines = list(c('Lagged DV', rep(c("Y", "Y", "N", "N","N"), 2)),
                                                c("Differenced", rep(c("N", "N", "Y", "Y", "Y"), 2)),
                                                c("Controls", rep(c("N", "Y", "N", "Y", "Y"), 2)),
                                                c("County FE", rep(rep(c("N", "Y"), c(4,1)), 2))
                               ),
                               label = "tab:twp_suffrage_robust_wi",
                               title = "Effect of Enlistment on Support for Black Suffrage (Wisconsin Township Returns)",
                               star.cutoffs = c(0.05,0.01,0.001),
                               float.env = 'sidewaystable'
)

note = 'Enlistment rate is number of men serving over those eligible to vote in 1865. Suffrage vote totals come from state constitutional referenda returns in stable clusters of townships/counties in Wisconsin. Control variables include: (i) the fraction eligible to vote in 1857 over those eligible to vote in 1865; and (ii) The fraction voting for the Republican gubernatorial candidate in 1857 minus the fraction voting for suffrage in 1857. Townships are weighted by number of white men. The restricted sample includes only townships where the population eligible to vote in 1865 changed by less than 50 percent between 1860 and 1870. Standard errors are robust.'
write_latex(table_c1, note, './output/appendix/Table_C1.tex')

#table 3 full
names(referenda_lm_wi) = c("Yes (Post)", "Yes (Diff.)") %>% rep(c(2,3)) %>% rep(2)

rows = rbind(c('Lagged DV', rep(c("Y", "Y", "N", "N","N"), 2)),
            c("Differenced", rep(c("N", "N", "Y", "Y", "Y"), 2)),
            c("Controls", rep(c("N", "Y", "N", "Y", "Y"), 2)),
            c("County FE", rep(rep(c("N", "Y"), c(4,1)), 2))
) %>% as.data.frame()

attr(rows, 'position') = 11:14

table_out = modelsummary(referenda_lm_wi,
                         coef_rename = c('vet_pct' = 'Enlistment Rate', 
                                         'suff_57_yes' = "Pro Suffrage (%) 1857",
                                         'elig_pct' = "Ratio of Elig. Voters (Pre/Post)",
                                         "gov_57_r_diff" = "Pro-Rep. - Pro-Suffrage (%) 1857"
                                         ),
                         coef_omit = "County_Cluster",
                         notes = note,
                         stars = T,
                         vcov = paste0("HC", c(3,1)) %>% rep(c(4,1)) %>% rep(2),
                         add_rows = rows,
                         gof_omit = "(Log)|(AIC)|(BIC)",
                         title = "Effect of Enlistment on Support for Black Suffrage (Wisconsin Township Returns)")  %>%
  add_header_above(c(" " = 1, "Full Sample" = 5, "Restricted Sample" = 5))
table_out = data.table(table = table_out, name = "Table C1")
table_list = c(table_list, list(table_out))


#####################
#Interaction Effects#
#####################

#set variables:
d_var = "vet_pct_s"
z_diff = NULL
z_ldv = c('suff_57_yes')
y_ldv = 'suff_65_yes'
y_diff = 'diff_suff'
fe = 'state'
weights = 'elig_1865'
bins = 3

###########
#Figure C4# 
###########

x_var = 'c_company_kia'
x_lab = 'Residual Company KIA'

use_mod = ia_wi_twp_table[, use_mod := c_company_kia %between% c(-1,1)]
inter_use = ia_wi_twp_table[(sample_1) & use_mod]

#LDV binning
a = inter.binning(Y = y_ldv, D = d_var, X = x_var, Z = z_ldv, FE = fe, 
                  data = inter_use, na.rm = T,
                  main = "Lagged DV", theme.bw = T, 
                  weights = weights,
                  nbins = bins,
                  Xlabel = x_lab)

#Diff bin
b = inter.binning(Y = y_diff, D = d_var, X = x_var, Z = z_diff, FE = fe, 
                  data = inter_use, na.rm = T, 
                  main = "Differenced", theme.bw = T, weights = weights, 
                  nbins = bins,
                  Xlabel = x_lab)

ylab = "Marginal Effect of Enlistment (%)"
xlab = paste("Moderator:", x_lab)

out_list =  list(a$graph, b$graph)
for (i in seq_along(out_list)) {
  out_list[[i]]$labels$x = NULL
  out_list[[i]]$labels$y = NULL
  out_list[[i]]$theme$plot.title$size = 15
}

p = grid.arrange( 
  arrangeGrob(grobs = out_list, 
              bottom=textGrob(xlab, gp=gpar(fontface="bold", fontsize=15)), 
              left=textGrob(ylab, gp=gpar(fontface="bold", fontsize=15), rot = 90), nrow = 1, ncol = 2) 
)

ggsave("./output/appendix/Figure_C4.pdf", p, width = 8.5, height = 6)


out_table = rbind(a$est.bin, b$est.bin) %>% .[, -1] %>% as.data.frame() %>% round(3)
names(out_table)[1] = "Enlistment Rate"

note = 'This reports the marginal effect of enlistment rates on support for Black Suffrage across mean experience of combat casualties. All models include state dummies. Enlistment rate is number of surviving men who enlisted over those eligible to vote in 1865. Residual Company KIA is the standardized difference between combat deaths in a soldier\'s company and the rest of his regiment, averaged by town.  Suffrage vote totals come from state constitutional referenda returns in 545 stable clusters of townships/counties in Wisconsin and Iowa. Townships are weighted by number of white men. Standard errors are robust.'

table_out = kable(out_table,
                  caption = "Marginal Effect of Enlistment Rates on Votes for Black Suffrage Conditional on
Company Casualty Rates") %>% 
  pack_rows(index = c("Lagged DV" = 3, "Differenced" = 3)) %>%
  kable_styling() %>%
  add_footnote(note)

table_out = data.table(table = table_out, name = "Figure C4")
table_list = c(table_list, list(table_out))

###########
#Figure C5# 
###########

x_var = 'c_usct_combat_days'
x_lab = 'Residual Combat Days with USCT'

ia_wi_twp_table[, use_mod := c_usct_combat_days %between% c(-1,1)]
inter_use = ia_wi_twp_table[(sample_1) & use_mod]

#LDV binning
a = inter.binning(Y = y_ldv, D = d_var, X = x_var, Z = z_ldv, FE = fe, 
                  data = inter_use, na.rm = T,
                  main = "Lagged DV", theme.bw = T, 
                  weights = weights,
                  nbins = bins, 
                  Xlabel = x_lab)

#Diff bin
b = inter.binning(Y = y_diff, D = d_var, X = x_var, Z = z_diff, FE = fe, 
                  data = inter_use, na.rm = T, 
                  main = "Differenced", theme.bw = T, weights = weights, 
                  nbins = bins, 
                  Xlabel = x_lab)

ylab = "Marginal Effect of Enlistment (%)"
xlab = paste("Moderator:", x_lab)

out_list =  list(a$graph, b$graph)
for (i in seq_along(out_list)) {
  out_list[[i]]$labels$x = NULL
  out_list[[i]]$labels$y = NULL
  out_list[[i]]$theme$plot.title$size = 15
}

p = grid.arrange( 
  arrangeGrob(grobs = out_list, 
              bottom=textGrob(xlab, gp=gpar(fontface="bold", fontsize=15)), 
              left=textGrob(ylab, gp=gpar(fontface="bold", fontsize=15), rot = 90), nrow = 1, ncol = 2) 
)

ggsave("./output/appendix/Figure_C5.pdf", p, width = 8.5, height = 6)


out_table = rbind(a$est.bin, b$est.bin) %>% .[, -1] %>% as.data.frame() %>% round(3)
names(out_table)[1] = "Enlistment Rate"

note = 'This reports the marginal effect of enlistment rates on support for Black Suffrage across mean experience of combat alongside USCT regiments. All models include state dummies. Enlistment rate is number of surviving men who enlisted over those eligible to vote in 1865. Residual USCT Combat is the standardized difference between days a soldier spent fighting alongside USCT units  compared to other soldiers who enlisted into the same branch of service and term of enlistment in the same year, averaged by town.  Suffrage vote totals come from state constitutional referenda returns in 545 stable clusters of townships/counties in Wisconsin and Iowa. Townships are weighted by number of white men. Standard errors are robust.'

table_out = kable(out_table, 
                  caption = "Marginal Effect of Enlistment Rates on Votes for Black Suffrage Conditional on
Days of Combat alongside USCT Regiments" ) %>% 
  pack_rows(index = c("Lagged DV" = 3, "Differenced" = 3)) %>%
  kable_styling() %>%
  add_footnote(note)

table_out = data.table(table = table_out, name = "Figure C5")
table_list = c(table_list, list(table_out))

##############################################################
#Cleanup
rm(list = setdiff(ls(), c(keep, 'keep')))
gc()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    